package org.light.simpleauth.verb;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Verb;
import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.JavascriptBlock;
import org.light.domain.JavascriptMethod;
import org.light.domain.Method;
import org.light.domain.Signature;
import org.light.domain.Statement;
import org.light.easyui.EasyUIPositions;
import org.light.exception.ValidateException;
import org.light.utils.WriteableUtil;
import org.light.verb.FindByName;
import org.light.verb.ListAll;

public class ReadMySession extends Verb implements EasyUIPositions {
	protected Domain roleDomain;
	protected Domain privilegeDomain;	

	@Override
	public Method generateDaoImplMethod() throws Exception {
		return null;
	}	

	@Override
	public Method generateDaoMethodDefinition() throws Exception {
		return null;		
	}

	@Override
	public Method generateServiceMethodDefinition() throws Exception {
		return null;
	}
	@Override
	public Method generateServiceImplMethod() throws Exception  {
		return null;
	}
	
	public ReadMySession(){
		super();
		this.setLabel("读出会话");
	}
	
	public ReadMySession(Domain domain) throws ValidateException{
		super();
		this.domain = domain;
		this.denied = domain.isVerbDenied("ReadMySession");
		this.setVerbName("ReadMySession");
		this.setLabel("读出会话");
		if  (domain.getLanguage().equalsIgnoreCase("english"))  this.setLabel("ReadMySession");
	}

	@Override
	public Method generateControllerMethod() throws Exception {
		if (this.denied) return null;
		else {
			Method method = new Method();
			method.setStandardName("ReadMySession");
			method.addSignature(new Signature(1,"c","*gin.Context"));
			
			List<Writeable> sList = new ArrayList<Writeable>();
			sList.add(new Statement(1000L,1,"username, err := session.ValidateJWTToken(c)"));
			sList.add(new Statement(2000L,1,"if err == nil && username != \"\" {"));
			FindByName find = new FindByName(this.domain);
			ListAll listAll = new ListAll(this.privilegeDomain);
			sList.add(new Statement(2000L,0,""+this.domain.getLowerFirstDomainName()+" := service."+find.getVerbName()+"(username)"));
			sList.add(new Statement(2100L,2,"roles := []string{\"admin\"}"));
			sList.add(new Statement(2200L,2,"perms := service."+listAll.getVerbName()+"()"));
			sList.add(new Statement(2300L,2,"c.JSON(http.StatusOK, gin.H{"));
			sList.add(new Statement(2400L,3,"\"user\":    "+this.domain.getLowerFirstDomainName()+","));
			sList.add(new Statement(2500L,3,"\"roles\":   roles,"));
			sList.add(new Statement(2600L,3,"\"perms\":   perms,"));
			sList.add(new Statement(2700L,3,"\"success\": true,"));
			sList.add(new Statement(2800L,2,"})"));
			sList.add(new Statement(2900L,2,"return"));
			
			sList.add(new Statement(6000L,1,"}"));
			sList.add(new Statement(7000L,1,"SendResponse(c, nil, nil, 0, false)"));

			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;
		}
	}	
	@Override
	public JavascriptBlock generateEasyUIJSButtonBlock() throws Exception {
		return null;	
	}

	@Override
	public JavascriptMethod generateEasyUIJSActionMethod() throws Exception {
		return null;		
	}

	public Domain getRoleDomain() {
		return roleDomain;
	}

	public void setRoleDomain(Domain roleDomain) {
		this.roleDomain = roleDomain;
	}

	public Domain getPrivilegeDomain() {
		return privilegeDomain;
	}

	public void setPrivilegeDomain(Domain privilegeDomain) {
		this.privilegeDomain = privilegeDomain;
	}
	
}
